package com.qiwenshare.file.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.api.ArchiveService;
import com.qiwenshare.file.component.InitData;
import com.qiwenshare.file.dto.archive.ApproveArchiveDTO;
import com.qiwenshare.file.vo.archive.ArchiveVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 档案信息表(Archive)表控制层
 *
 * @author makejava
 * @since 2023-10-08 21:09:12
 */
@RestController
@RequestMapping("portalArchive")
public class PortalArchiveController {
    /**
     * 服务对象
     */
    @Resource
    private ArchiveService archiveService;

    public static final String CURRENT_MODULE = "档案管理";

    @Operation(summary = "分页查询档案", description = "分页查询档案", tags = {CURRENT_MODULE})
    @GetMapping(value = "/page")
    @MyLog(operation = "分页查询档案", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<ArchiveVo> queryPage(
            @Parameter(description = "档案名称", required = false) String archiveName,
            @Parameter(description = "素材类型", required = false) Integer fileType,
            @Parameter(description = "档案类型", required = false) String archiveTypeId,
            @Parameter(description = "档案编号", required = false) String archiveCode,
            @Parameter(description = "归属部门", required = false) String departmentId,
            @Parameter(description = "开始时间", required = false) String startDate,
            @Parameter(description = "结束时间", required = false) String endDate,
            @Parameter(description = "当前页", required = true) long currentPage,
            @Parameter(description = "页面数量", required = true) long pageCount) {
        IPage<ArchiveVo> page = archiveService.getListWithApprove(archiveName, fileType, archiveTypeId, archiveCode,  departmentId, startDate, endDate , currentPage, pageCount);
        List<ArchiveVo> list = page.getRecords().stream().peek(u -> {
            u.setDepartment(InitData.getDepartmentNameById(u.getDepartmentId()));
            u.setCreateUser(InitData.getStaffNameByUserId(u.getCreateUserId()));
            u.setModifyUser(InitData.getStaffNameByUserId(u.getModifyUserId()));
        }).collect(Collectors.toList());
        return RestResult.success().dataList(list, page.getTotal());
    }

    @Operation(summary = "申请档案下载", description = "申请档案下载", tags = {CURRENT_MODULE})
    @PostMapping(value = "/approve")
    @MyLog(operation = "申请档案下载", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<Boolean> approve(@RequestBody ApproveArchiveDTO approveArchiveDTO) {
        return RestResult.success().data(archiveService.approve(approveArchiveDTO.getArchiveId(), SessionUtil.getUserId()));
    }

}
